<?php
class Membre extends WotHub 
{
    private $_model_membre;
    private $_model_clan;
    private $_clanId;
    private $_role;
    
    public function Deconnexion() {
        setcookie('account_id', '',mktime(0,0,0,1,1,1900),'/');
        session_destroy();
        setcookie(session_name(), '', time() - 42000);
        header('Location:'.$this->baseUri);
    }
    
    public function Accueil() {
        $this->_pdo = new smartbokModele($this->bdd);
        $this->addTemplate('indexclan.tpl.php');
        $this->_assignSession('accountInfo');
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $this->msgAccueil = nl2br($clan->getMessageAccueil());
        $this->_pdo = null;
    }

    public function afficherGarage() {
        $this->_pdo = new smartbokModele($this->bdd);
        $this->ajouterScript('js/garage.js');
        $this->titreBandeau = 'Garage de ';
    	$accountId = $this->membre_id;
        $this->addTemplate('garage.tpl.php');
        $this->_assignSession('accountInfo');
        $appId = $this->appId;

        $this->_model_membre = new Model_Membre($this->_pdo, $appId);
        $listeTanks = $this->_model_membre->getListeTank($accountId);
        $this->_model_membre->insertPersonalStats($accountId, $listeTanks);

        $garage = new Model_Garage($this->_pdo, $appId);
        $tanksActif = $garage->getTanksAuGarage($accountId);
        $this->score = true;
        $tanksReserve = $garage->getTanksEnReserve($accountId);
        $tanksNouveaux = array();
        $nouveaux = 0;
        foreach ($listeTanks as &$tank) {
            $isNew = false;
        	if (array_key_exists($tank['tank_id'], $tanksActif) === false && array_key_exists($tank['tank_id'], $tanksReserve) === false) {
        		$newTank = new Model_Tank($this->_pdo, $appId);
        		$tankInfo = $newTank->getTankInfo($tank['tank_id']);        		
        		$tanksNouveaux[$tank['tank_id']] = $tankInfo;
        		$new[] = $tank['tank_id'];
        		$isNew = true;
        		$nouveaux++;
        	}
        	Model_Tank::activerTank($accountId, $tank['tank_id'], $tank['in_garage'],$isNew);
        } 

        if ($nouveaux > 0) {
            $tanksActif = $garage->getTanksAuGarage($accountId);
            $this->new = $new;
        }
        foreach ($tanksActif as &$tank) {
            $tankActif = new Model_Tank($this->_pdo, $_SESSION['appId']);
            $tank['skills'] = $tankActif->getCrewSkills($tank['tank_id'], $accountId);
            $tank['crew'] = $tankActif->getCrew($tank['tank_id']);
            $tank['score'] = $tankActif->getScore($tank['tank_id'], $accountId);
        }
        $this->tanks = $tanksActif;
        $this->_pdo = null;
    }
    
    public function AfficherReserve() {
        $this->_pdo = new smartbokModele($this->bdd);
        $this->titreBandeau = 'Reserve de ';
        $this->ajouterScript('js/garage.js');
        $account_id = $this->membre_id;
        $this->addTemplate('garage.tpl.php');
        $this->_assignSession('accountInfo');
        $appId = $this->appId;
        $this->score = false;
        $this->_model_membre = new Model_Membre($this->_pdo, $appId);
        $garage = new Model_Garage($this->_pdo, $appId);
        
        $tanksReserve = $garage->getTanksEnReserve($account_id);
        
        $this->tanks = $tanksReserve;
    }
    
    public function Competence() {
        $this->_assignSession('accountInfo');
        $this->ajouterScript('js/skills.js');
        $params = $this->getParams();
        if (count($params) === 1 ) {
            $tank_id = $params[0];
        }
        $this->_pdo = new smartbokModele($this->bdd);
        $this->addTemplate('competences.tpl.php');
        
        $tankActif = new Model_Tank($this->_pdo, $this->appId);
        $tank = $tankActif->getTankInfo($tank_id);
        if (isset($_POST['tank_id']) === true) {
            $tankActif->enregistrerSkills();
        }
        $this->tank = $tank[0];
        
        $this->equipage = $this->_prepareEquipage($tank_id, $tankActif);
        $tabSkills = $this->_prepareSkills($tankActif, $this->equipage, $tank_id);
        $this->skills = $tabSkills[0];
        $this->crewSkills = $tabSkills[1];
    }
    
    private function _prepareSkills($tankActif, $crew, $tank_id){
        $crewSkills = $tankActif->getCrewSkills($tank_id, $_SESSION['account_id']);
        $listeSkills = $tankActif->getSkills();
        $tabSkill = array();
        foreach ($listeSkills as $skill) {
            $role = null;
            $addSkill['image'] = $skill['image'];
            $addSkill['id'] = $skill['skill_id'];
            $addSkill['title'] = $skill['name'];
            foreach ($crew as $membre) {
                if ($membre['id'] === $skill['role_id'] || $skill['role_id'] === '' || strstr($membre['role_ad'],$skill['role_id']) !==false ) {
                    $valeur = 'ok';
                    foreach($crewSkills as $crewSkill ){
                        if ($crewSkill['crew_id'] === $membre['ordre'] && $crewSkill['skill_id'] === $skill['skill_id']) {
                            $valeur = $crewSkill['valeur'];
                        }
                    }
                    $role[$membre['ordre']] = $valeur;
                }
                else {
                    $role[$membre['ordre']] = '';
                }
            }
            $addSkill['roles'] = $role;
            $skills[] = $addSkill;
        }
        $tabSkills[] = $skills;
        $tabSkills[] = $crewSkills;
        return $tabSkills;
    }

    private function _prepareEquipage($tank_id, $tankActif) {
        $crew = $tankActif->getCrew($tank_id);
        $roles = $tankActif->getRole();
        $equipage = array();
        foreach ($crew as $membre) {
            $addRole = array();
            $carac['id'] = $membre['role_id'];
            $carac['role'] = $roles[$membre['role_id']]['libelle'];
            $carac['image'] = $roles[$membre['role_id']]['image'];
            $carac['role_ad'] = $membre['role_ad'];
            $carac['ordre'] = $membre['ordre'];
            $roleAd = explode(',', $membre['role_ad']);
            $carac['title'] = '';
            foreach ($roleAd as $role) {
                if ($role !== '') {
                    $addRole[] = $roles[$role]['libelle'];
                }
            }
            $carac['title'] = str_replace('()', '', $carac['role'].' ('.implode(', ', $addRole).')');
            $equipage[] = $carac;
        }
        return $equipage;
    }
    
    public function AfficherStats() {
        $this->sens = 'desc';
        $this->cleTri = 'battles';
    if (isset($_POST['frm_col']) === true) {
            $this->cleTri = $_POST['frm_col'];
            $this->sens = $_POST['frm_sens'];
        }
        if (isset($_POST['frm_action']) === true && $_POST['frm_action'] === 'Reset') {
            $_POST = array();
        }
        $this->ajouterScript('js/garage.js');
        $this->addTemplate('personalstats.tpl.php');
        $this->_model_membre = new Model_Membre($this->_pdo,$this->appId);
        $tanks = $this->_model_membre->getListeTank($this->membre_id);
        $this->dataTanks = Model_Tank::getAllDataTank();
        $expTankValues = Ratings::getExpValues(); 
        
        foreach ($tanks as &$tank) {
            $avoidTank = false;
            $tank['all']['tiers'] = $this->dataTanks[$tank['tank_id']]['level'];
            if (isset($_POST['tiers']) === true && in_array($tank['all']['tiers'], $_POST['tiers']) === false) {
                $tank = null;
                $avoidTank = true;
            }
            if (isset($_POST['frm_all']) === true && ($tank['in_garage'] != $_POST['frm_all'] && $_POST['frm_all'] !== 'all')) {
                $tank = null;
                $avoidTank = true;
            }
            if ($avoidTank === false) {
                $tank['all']['winRate'] = 0;
                if ($tank['all']['battles'] > 0) {
                    $tank['all']['winRate'] = round($tank['all']['wins']/$tank['all']['battles']*100,2);
                }
                $tank['name'] = $this->dataTanks[$tank['tank_id']]['name'];
                
                if (isset($expTankValues[$tank['tank_id']]) === true) {
                    $tank['exp'] = Ratings::setTankExpStat($expTankValues[$tank['tank_id']], $tank);
                    $tank['WN8'] = Ratings::calculerTankWN8($expTankValues[$tank['tank_id']],$tank['all']);
                    $tank['WN8_status'] = Ratings::getRatingRang('WN8', $tank['WN8']);
                }
                else {
                    $tank['WN8'] = '--';
                    $tank['exp'] = $tank['all'];
                    $tank['WN8_status'] = '';
                }
                
                $tank['ER'] = Ratings::calculerER($tank['all']);
                $tank['ER_status'] = Ratings::getRatingRang('ER', $tank['ER']);
                $tank['WN7'] = Ratings::calculerWN7($tank['all']);
                $tank['WN7_status'] = Ratings::getRatingRang('WN7', $tank['WN7']);
                foreach ($tank['all'] as $key => &$stat) {
                    if ($key === 'winRate') {
                        $stat = number_format($stat, 2, ',' , ' ');
                    }
                    else {
                        $stat = number_format($stat, 0, ',' , ' ' );
                    }
                }
            }
        }
        $this->listeTanks = $this->_triTank($tanks);
    }
    
    private function _triTank($tanks) {
        $cleTri = 'battles';
        if (isset($_POST['frm_col']) === true) {
            $cleTri = $_POST['frm_col'];
        }
        foreach ($tanks as $tank) {
            if (isset($tank['name']) === true) {
                if (isset($tank['all'][$cleTri]) === true) {
                    $dispatch[(float)$tank['all'][$cleTri]][$tank['name']] = $tank;
                }
                else {
                    $dispatch[(float)$tank[$cleTri]][$tank['name']] = $tank;
                }
            }
        }
        if (isset($_POST['frm_sens']) === true && $_POST['frm_sens'] === 'asc') {
            ksort($dispatch);
        }
        else {
            krsort($dispatch);
        }
        foreach ($dispatch as &$tanks) {
            ksort($tanks);
            foreach ($tanks as $tank) {
                $tanksTries[] = $tank;
            }
        }
        return $tanksTries;
    }
}